package com.example.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.app.entity.Song;
import com.example.app.service.ISongService;
import com.example.common.config.ConfigProperties;
import com.example.common.config.TransWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class SearchController {

    @Autowired
    private ConfigProperties configProperties;

    @Autowired
    private ISongService songService;


    @GetMapping("/search")
    public Map<String, Object> search(HttpServletRequest request){

        Map<String, Object> result = new HashMap<>();

        QueryWrapper<Song> wrapper = new QueryWrapper<>();

        wrapper.select("id", "name");
        String keywords = "周杰伦";
        String temp = request.getParameter("keywords");
        if(StringUtils.hasText(temp)){
            keywords = temp;
        }
        wrapper.like("name", keywords);

        List<Song> songs = songService.list(wrapper);

        result.put("songs", songs);

        return TransWrapper.wrap("result", result);
    }

    @GetMapping("/search/default")
    public Map<String, Object> searchDefault(HttpServletRequest request){

        HashMap<String, Object> data = new HashMap<>();

        data.put("showKeyword","周杰伦-夜曲");

        return  TransWrapper.wrap("data", data);
    }

    @GetMapping("/search/hot/detail")
    public Map<String, Object> searchHotDetail(HttpServletRequest request){

        ArrayList<HotDetail> hotDetails = new ArrayList<>();
        String hotImg = configProperties.getNginxBasePath()+"/img/hot.png";
        hotDetails.add(new HotDetail("偏爱",hotImg));
        hotDetails.add(new HotDetail("游京",hotImg));
        hotDetails.add(new HotDetail("会不会",""));
        hotDetails.add(new HotDetail("海底",hotImg));
        hotDetails.add(new HotDetail("好运来",""));
        hotDetails.add(new HotDetail("猪猪侠",""));
        hotDetails.add(new HotDetail("张杰",""));
        hotDetails.add(new HotDetail("竞技场",""));
        hotDetails.add(new HotDetail("手扶拖拉机司机",""));

        return  TransWrapper.wrap("data", hotDetails);

    }

    public static class HotDetail{

        private String searchWord;

        private String iconUrl;

        public HotDetail(String searchWord, String iconUrl) {
            this.searchWord = searchWord;
            this.iconUrl = iconUrl;
        }

        public String getSearchWord() {
            return searchWord;
        }

        public void setSearchWord(String searchWord) {
            this.searchWord = searchWord;
        }

        public String getIconUrl() {
            return iconUrl;
        }

        public void setIconUrl(String iconUrl) {
            this.iconUrl = iconUrl;
        }
    }

}
